﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using P_PersonalDetails.Service.Qualifications;
using System.Collections.ObjectModel;
using System.Windows.Data;
namespace P_PersonalDetails
{
    public partial class Qualifications : UserControl
    {
        public Qualifications()
        {
            InitializeComponent();
            try
            {
                SRV.GetQualificationsCompleted += new EventHandler<GetQualificationsCompletedEventArgs>(SRV_GetQualificationsCompleted);
            }
            catch (Exception)
            {
            }
        }

        void SRV_GetQualificationsCompleted(object sender, GetQualificationsCompletedEventArgs e)
        {
            foreach (var item in e.Result.SelectMany((r)=>r.Qualifications))
            {
                item.PropertyChanged +=new System.ComponentModel.PropertyChangedEventHandler(item_PropertyChanged);
            }
            this.DataContext = e.Result;
            SMemberID = (int)e.UserState;
            SQualifications = e.Result;
        }

        void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SRV.UpdatePropertyAsync((int)sender.GetPropertyValue("ID"), e.PropertyName, sender.GetPropertyValue(e.PropertyName));
        }

        public int? MemberID
        {
            get { return (int?)GetValue(MemberIDProperty); }
            set { SetValue(MemberIDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MemberID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MemberIDProperty =
            DependencyProperty.Register("MemberID", typeof(int?), typeof(Qualifications), new PropertyMetadata(null, (DO, DE) =>
            {
                if (DE.NewValue != null)
                {
                    ((Qualifications)DO).MemberIDChanged();
                }
            }));

        void MemberIDChanged()
        {
            SRV.GetQualificationsAsync(MemberID.Value, MemberID.Value);
        }

        QualificationServiceClient SRV = ServiceConfig.NewQualificationClient();

        public static int SMemberID { get; set; }
        public static ObservableCollection<QualificationServiceQualificationsData> SQualifications { get; set; }
    }


    public class AddQualificationCommand : ICommand
    {

        #region ICommand Members
        public AddQualificationCommand()
        {
            try
            {
                SRV.NewCompleted += new EventHandler<NewCompletedEventArgs>(SRV_NewCompleted);
            }
            catch
            {
            }
            
        }

        void SRV_NewCompleted(object sender, NewCompletedEventArgs e)
        {
            var Category = Qualifications.SQualifications.First((Q) => Q.CodeType == (int)e.UserState);
            var Qu = new QualificationServiceQualificationData()
            {
                ID = e.Result,
                CategoryCT = Category.CodeType,
                HasExpiryDate = Category.HasExpiryDate
            };
            Category.Qualifications.Add(Qu);
            Qu.PropertyChanged +=new System.ComponentModel.PropertyChangedEventHandler(Q_PropertyChanged);
        }

        void Q_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SRV.UpdatePropertyAsync((int)sender.GetPropertyValue("ID"), e.PropertyName, sender.GetPropertyValue(e.PropertyName));
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged { add { } remove { } }

        public void Execute(object parameter)
        {
            SRV.NewAsync(Qualifications.SMemberID, (int)parameter, parameter);
        }
        #endregion


        QualificationServiceClient SRV = ServiceConfig.NewQualificationClient();
    }

    public class DeleteQualificationCommand : ICommand
    {

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged { add { } remove { } }

        public void Execute(object parameter)
        {
            SRV.DeleteAsync((int)parameter);
            var List = Qualifications.SQualifications.First((Q) => Q.Qualifications.Any((Qu) => Qu.ID == (int)parameter));
            List.Qualifications.Remove(List.Qualifications.First((Q) => Q.ID == (int)parameter));
        }
        #endregion

        QualificationServiceClient SRV = ServiceConfig.NewQualificationClient();
    }

    public class VisibilityConverter:IValueConverter
    {

        #region IValueConverter Members

        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (Convert.ToBoolean(value.ToString())) return Visibility.Visible;
            }
            catch { }
            return Visibility.Collapsed;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
    public class InverseVisibilityConverter : IValueConverter
    {

        #region IValueConverter Members

        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (Convert.ToBoolean(value.ToString())) return Visibility.Collapsed;
            }
            catch { }
            return Visibility.Visible;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
